package com.ruoyi.web.controller.warning;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.warning.domain.entity.WarningSyslogConfiguration;
import com.ruoyi.warning.domain.query.ConfigurationQuery;
import com.ruoyi.warning.service.IWarningSyslogConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 告警配置表 前端控制器
 * </p>
 *
 * @author chenhc
 * @since 2025-07-31
 */
@RestController
@RequestMapping("/warningSyslogConfiguration")
@Component("warningSyslogConfiguration")
@Api(tags = "日志告警配置", description = "用于处理日志告警配置相关操作的控制器")
public class WarningSyslogConfigurationController {
    @Resource
    private IWarningSyslogConfigurationService warningSyslogConfigurationService;

    @ApiOperation("分页")
    @Log(title = "查询告警配置", businessType = BusinessType.SELECT)
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping(value = "/page")
    public R<TableDataInfo> page(ConfigurationQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<WarningSyslogConfiguration> list = warningSyslogConfigurationService.list(
                new LambdaQueryWrapper<WarningSyslogConfiguration>()
                        .eq(!ObjectUtils.isEmpty(query.getDeviceId()),WarningSyslogConfiguration::getDeviceId, query.getDeviceId())
                        .orderByDesc(WarningSyslogConfiguration::getCreateTime)
        );
        return R.ok(new TableDataInfo(list, page.getTotal()));
    }

    @ApiOperation("详情")
    @Log(title = "告警配置详情", businessType = BusinessType.SELECT)
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping(value = "/details/{id}")
    public R getDetails(@PathVariable String id) {
        return R.ok(warningSyslogConfigurationService.getById(id));
    }

    @ApiOperation("新增或修改")
    @Log(title = "新增或修改告警配置", businessType = BusinessType.INSERT)
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping(value = "/saveOrUpdate")
    public R saveConfiguration(@RequestBody WarningSyslogConfiguration configuration) {
        return R.ok(warningSyslogConfigurationService.saveOrUpdate(configuration));
    }

    @ApiOperation("删除")
    @Log(title = "删除告警配置", businessType = BusinessType.DELETE)
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping (value = "/delete")
    public R deleteConfiguration(@RequestBody List<String> ids) {
        warningSyslogConfigurationService.removeByIds(ids);
        return R.ok("success");
    }

    @Anonymous
    @ApiOperation("刷新日志并告警")
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping (value = "/refresh")
    public R refresh() throws Exception {
        warningSyslogConfigurationService.refresh();
        return R.ok();
    }
}
